package com.wtwd.campus.receiver;

import com.wtwd.campus.entity.MqAllWhiteCallResult;
import com.wtwd.campus.entity.MqLocationData;
import com.wtwd.campus.service.IRMFeignClientService;
import com.wtwd.campus.service.InjectFeignService;
import com.wtwd.campus.utils.LocationUtils;
import com.wtwd.campus.utils.UnicodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SetAllWhiteListNumberReceiver implements Receiver {
    @Autowired
    private IRMFeignClientService irmFeignClientService;
    @Override
    public String parseMessage(String data) {
        // 两条的回复   *WT,866248050395985,V4,PBWLALL,144937,1,18797874506,1,0,\u0074\u0065\u0073\u0074,1,,2,18797874507,1,0,\u0074\u0065\u0073\u0074\u0031,1,,144939,A,2250.2214,N,11391.6105,E,0.18,281,030621,FFFFDFFF#
        // 一条的回复    *WT,866248050395985,V4,PBWLALL,144854,1,18797874506,1,0,\u0074\u0065\u0073\u0074,1,,144855,A,2250.2214,N,11391.6105,E,0.18,281,030621,FFFFDFFF#
        // *WT,866248050395985,V4,PBWLALL,141920,1,12487541256,1,0,\u0074\u0065\u0073\u0074,1,141922,V,0.0000,S,0.0000,W,0.00,0,030621,FFFDFFFF#
        String[] split = data.split(",");
        int length=split.length;
        String imei=split[1];
        String messageType = split[2];
        String type = split[3];
        String commandNumber=split[4];
        int index=(length-15)/7;//总的长度减去指令中其他信息，算出有几组号码信息
        //存放所有的亲情号码信息
        List<Integer> sequenceList = new ArrayList<>();
        List<Map<String,String>> whiteList=new ArrayList<>();
        for(int j=1;j<=index;j++){
            //假如是第一组 split[5]
            sequenceList.add(Integer.parseInt(split[4+(j-1)*7+1]));
            Map<String,String> map=new HashMap<>();
            //假如是第一组 split[5]
             map.put("wlNum",split[4+(j-1)*7+1]);
            map.put("wlPhone",split[4+(j-1)*7+2]);
            map.put("wlType",split[4+(j-1)*7+3]);
            map.put("wlMeid",split[4+(j-1)*7+4]);
            String name = split[4+(j-1)*7+5];
            map.put("wlName", StringUtils.isNotBlank(name) ? UnicodeUtil.decodeUnicode(split[4+(j-1)*7+5]) : "" );
            map.put("wlPic",split[4+(j-1)*7+6]);
            whiteList.add(j-1,map);
        }
        //获取所有的定位信息
        //时间时分秒
        String time=split[length-10];
        String lat = split[length-8];
        String lng = split[length-6];
        String speed = split[length-4];
        String orientation = split[length-3];
        //年月日
        String date=split[length-2];
        //设备状态
        String deviceStatus=split[length-1];
        List<Double> list = LocationUtils.transiformLatAndLng(lng, lat);
        MqAllWhiteCallResult mqAllWhiteCallResult = new MqAllWhiteCallResult();
        mqAllWhiteCallResult.setCommandNumber(commandNumber);
        mqAllWhiteCallResult.setDeviceDate(date);
        mqAllWhiteCallResult.setDeviceTime(time);
        mqAllWhiteCallResult.setImei(imei);
        mqAllWhiteCallResult.setSequenceList(sequenceList);
        mqAllWhiteCallResult.setWhiteList(whiteList);
        MqLocationData mqLocationData = new MqLocationData();
        mqLocationData.setOrientation(Integer.parseInt(orientation));
        mqLocationData.setSpeed(Double.parseDouble(speed));
        mqLocationData.setLatitude(list.get(1));
        mqLocationData.setLongitude(list.get(0));
        mqAllWhiteCallResult.setMqLocationData(mqLocationData);
        InjectFeignService.getIrmFeignClientService().allWhiteCallResponse(mqAllWhiteCallResult);
        return imei;
    }
}
